package cn.itcast.order.service;

import cn.itcast.feign.clients.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;



    //用fegin进行远程调用来发出http请求
    /**
     * fegin非常强大,不仅可以实现远程调用,还能实现负载均衡
     */
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId){
        //1 查看订单
        Order order = orderMapper.findById(orderId);

        //2 用fegin远程调用
        User user = userClient.findById(order.getUserId());

        //3 封装user到order
        order.setUser(user);

        //4 返回
        return order;
    }


    //用RestTemplate发起http请求
    //@Autowired
    //private RestTemplate restTemplate;
    //
    //public Order queryOrderById(Long orderId) {
    //    // 1.查询订单
    //    Order order = orderMapper.findById(orderId);
    //    // 2.利用RestTemplate发起http请求,查询用户
    //    // 2.1 拼接路径请求地址
    //    String url = "http://userserver/user/" + order.getUserId();
    //    // 2.2 发送http请求,获取返回值
    //    //getForObject方法是用来发起HTTP GET请求,获取指定URL的响应数据并将其转换为Java对象。
    //    User user = restTemplate.getForObject(url, User.class);
    //
    //    //3.将user封装进order
    //    order.setUser(user);
    //
    //    // 4.返回
    //    return order;
    //}
}
